define(['jquery', 'bootstrap', 'backend', 'table', 'form','vue','echarts', 'echarts-theme',], function ($, undefined, Backend, Table, Form,Vue, Echarts, undefined) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        index: function () {
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');
            var option = {
                title: {text: '', subtext: ''},
                tooltip: {trigger: 'axis'},
                legend: {data: ['收益','支出']},
                toolbox: {
                    show: false,
                    feature: {magicType: {show: true, type: ['stack', 'tiled']}, saveAsImage: {show: true}}
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: Config.statistics.columns
                },
                yAxis: {},
                grid: [{left: 'left', top: 'top', right: '10', bottom: 30}],
                series: [{
                    name: '收益',
                    type: 'line',
                    smooth: true,
                    areaStyle: {normal: {}},
                    lineStyle: {normal: {width: 1.5}},
                    data: Config.statistics.earninglist
                }, {
                    name: '支出',
                    type: 'line',
                    smooth: true,
                    areaStyle: {normal: {}},
                    lineStyle: {normal: {width: 1.5}},
                    data: Config.statistics.spendinglist
                }]
            };
            myChart.setOption(option);
            $(window).resize(function () {
                myChart.resize();

            });
            $(document).on("click", ".btn-refresh", function () {
                $(".datetimerange").trigger("blur");
            });
            Form.api.bindevent($("form[role=form]"));
            $(document).on("click", ".btn-filter", function () {
                var label = $(this).text();
                var obj = $(".datetimerange").data("daterangepicker");

                var dates = obj.ranges[label];
                obj.startDate = dates[0];
                obj.endDate = dates[1];

                obj.clickApply();
            });
            $(".datetimerange").on("blur", function () {
                Fast.api.ajax({
                        url: 'wanlshop/account/index',
                        data: {date: $(this).val()}
                    }, function (data) {
                        var option = {
                            title: {text: '', subtext: ''},
                            tooltip: {trigger: 'axis'},
                            legend: {data: ['收益','支出']},
                            toolbox: {
                                show: false,
                                feature: {magicType: {show: true, type: ['stack', 'tiled']}, saveAsImage: {show: true}}
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: data.columns
                            },
                            yAxis: {},
                            grid: [{left: 'left', top: 'top', right: '10', bottom: 30}],
                            series: [{
                                name: '收益',
                                type: 'line',
                                smooth: true,
                                areaStyle: {normal: {}},
                                lineStyle: {normal: {width: 1.5}},
                                data: data.earninglist
                            }, {
                                name: '支出',
                                type: 'line',
                                smooth: true,
                                areaStyle: {normal: {}},
                                lineStyle: {normal: {width: 1.5}},
                                data: data.spendinglist
                            }]
                        };
                        myChart.setOption(option);
                        return false;
                    }
                );
            })
            //每隔一分钟定时刷新图表
            setInterval(function () {
                $(".btn-refresh").trigger("click");
            }, 60000);
            $(document).on("click", ".btn-withdraw", function () {
                var that = this;
                var id = "withdraw-tpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "提现",
                    area: ["400px", "250px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        Form.api.bindevent(form, function (data) {
                            location.reload();
                            Layer.closeAll();
                        });
                    }
                });
            });
            $(document).on("click",".btn-withdraw-log",function () {
                Fast.api.open('/index/wanlshop.account/withdrawlist.html','提现记录',{
                    area: ["80%", "80%"],
                });
            })
        },
        profile: function () {
            // 给上传按钮添加上传成功事件
            $("#faupload-avatar").data("upload-success", function (data) {
                var url = Fast.api.cdnurl(data.url);
                $(".profile-user-img").prop("src", url);
                Toastr.success(__('Uploaded successful'));
            });
            Form.api.bindevent($("#profile-form"));
            $(document).on("click", ".btn-change", function () {
                var that = this;
                var id = $(this).data("type") + "tpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "修改",
                    area: ["400px", "250px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        Form.api.bindevent(form, function (data) {
                            location.reload();
                            Layer.closeAll();
                        });
                    }
                });
            });

            $(document).on("click", ".change-pwd", function () {
                Layer.open({
                    type: 2,
                    title: "修改",
                    area: ["50%", "50%"],
                    content: 'changepwd',
                    end: function (layero) {
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000);
                    }
                });
            })
        },

        changepwd: function () {
            //本地验证未通过时提示
            $("#changepwd-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#changepwd-form"), function (data, ret) {
                setTimeout(function () {
                    parent.window.location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
        },
        bank:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/account/bank' + location.search,
                    add_url: 'wanlshop/account/bankadd',
                    //edit_url: 'wanlshop/account/bankedit',
                    del_url: 'wanlshop/account/bankdel',
                    multi_url: 'wanlshop/account/multi',
                    import_url: 'wanlshop/account/import',
                    table: 'wanlshop_pay_account',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'bankName',width:'500px', title: '账户', operate: 'LIKE',formatter: function (value,row,index) {
                            var background = "linear-gradient(45deg, rgb(87, 198, 201), rgb(37, 148, 201))";
                            var img = '/assets/img/'+row.bankCode+'.png';
                            return "<div style='background: "+background+"' class=\"panel panel-default panel-intro panel-statistics\">\n" +
                                "            <div class=\"panel-body\">\n" +
                                "                <div class=\"col-md-3 col-sm-3 col-xs-2 text-center user-center\">\n" +
                                "                    <div class=\"avatar-div\"><img class=\"avatar-img\" src='"+img+"' alt=\"\"></div>\n" +
                                "                </div>\n" +
                                "                <div class=\"col-md-9 col-sm-9 col-xs-10\">\n" +
                                "                    <!-- Content -->\n" +
                                "                    <div class=\"ui-content\">\n" +
                                "                        <!-- Heading -->\n" +
                                "                        <h4>"+row.bankName+"</h4>\n" +
                                "                        <!-- Paragraph -->\n" +
                                "                        <p>\n" +
                                "                            储蓄卡\n" +
                                "                        </p>\n" +
                                "                        <!-- Success -->\n" +
                                "                    </div>\n" +
                                "                </div>\n" +
                                "                <div class=\"col-md-12 col-sm-12 col-xs-12\">\n" +
                                "                    <span class=\"pull-right\">"+row.cardCode+"</span>\n" +
                                "                </div>\n" +
                                "            </div>\n" +
                                "        </div>"
                            }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        bankadd:function(){
            $(document).on("change", ".selectpicker-bank", function () {
               $('#c-bankName').val($('.selectpicker-bank option:checked').text());
            })
            Controller.api.bindevent();
        },
        bankedit:function(){
            $(document).on("change", ".selectpicker-bank", function () {
                $('#c-bankName').val($('.selectpicker-bank option:checked').text());
            })
            Controller.api.bindevent();
        },
        withdrawlist:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/account/withdrawlist',
                    //add_url: 'wanlshop/account/bankadd',
                    //edit_url: 'wanlshop/account/bankedit',
                    //del_url: 'wanlshop/account/bankdel',
                    //multi_url: 'wanlshop/account/multi',
                    //import_url: 'wanlshop/account/import',
                    table: 'wanlshop_pay_account',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('账户类型'),formatter: function (value,row,index) {
                                return "<div class=\"avatar-div\"><img class=\"avatar-img\" style='width: 50px' src='/assets/img/"+value+".png' alt=\"\"></div>"
                            }},
                        {field: 'account', title: __('账户')},
                        {field: 'status', title: __('状态'),searchList:{'created':'申请中','successed':'成功','rejected':'拒绝'},formatter: function (value,row,index) {
                                if(value =='created'){
                                    return "<span class=\"label label-primary\">申请中</span>"
                                }else if(value =='successed'){
                                    return "<span class=\"label label-success\">成功</span>"
                                }else{
                                    return "<span class=\"label label-danger\">拒绝</span><br>" +
                                        "("+row.memo+")"
                                }
                            }},
                        {field: 'createtime', title: __('申请时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});